using System;
using System.Collections;
using I2.Loc;
using UnityEngine;

public class RocketLeagueCotroller : MonoBehaviour
{
	public static RocketLeagueCotroller instance;

	public LayerMask mask;

	public GameObject rocketLeagueStadium;

	public RocketLeagueBall rocketLeagueBall;

	public Transform rocketLeagueBallSpawnPoint;

	public Vector3 ballSpace;

	public GameObject ballExplosionPrefab;

	public GameObject goalObjectRed;

	public GameObject goalObjectBlue;

	public Color myTeamColor;

	public Color enemyTeamColor;

	public Color goldColor;

	public int goalSeries;

	[Header("Rocket League 2 Goal")]
	public GameplayEvent rocketLeagueGoal_1;

	[Header("Rocket League 2 Goals")]
	public GameplayEvent rocketLeagueGoal_2;

	[Header("Rocket League 3 Goals")]
	public GameplayEvent rocketLeagueGoal_3;

	[Header("Rocket League 4 Goals")]
	public GameplayEvent rocketLeagueGoal_4;

	[Header("Rocket League 5 Goals")]
	public GameplayEvent rocketLeagueGoal_5;

	[Header("Rocket League 6 Goals")]
	public GameplayEvent rocketLeagueGoal_6;

	[Header("Rocket League Goal Save")]
	public GameplayEvent rocketLeagueGoal_Save;

	private float lastGoalTime;

	public bool rocketLeagueStarted;

	public bool inRestartWindow;

	private float explosionRadius = 350f;

	private float explosionForce = 50000f;

	private float explosionUp = 20000f;

	private ForceMode explosionForceMode = ForceMode.Impulse;

	private CarBehavior myCar;

	private void Awake()
	{
		instance = this;
		if (GameController.instance.curTypeGame == TypeGame.RocketLeague)
		{
			goalObjectRed.SetActive(false);
			goalObjectBlue.SetActive(false);
		}
	}

	public unsafe void StartGame()
	{
		if (GameController.instance.curTypeGame == TypeGame.RocketLeague && !rocketLeagueStarted && PVPManager.instance.gameIsStarted)
		{
			goalSeries = 0;
			rocketLeagueStarted = true;
			inRestartWindow = false;
			(GameController.instance.CurrentGameMode as RocketLeagueGameMode).StartRoundWithAction(new Action(this, (IntPtr)__ldftn(RocketLeagueCotroller.StartNewRound)));
		}
	}

	public void OnRestartGame()
	{
		if (!PVPManager.instance.timerStart)
		{
			rocketLeagueBall.ballPhotonView.TransferOwnership(PlayerBehavior.MyPlayer.ID);
			RespawnBall();
		}
		if ((bool)myCar)
		{
			myCar.freeBoostAmount = 0f;
		}
		rocketLeagueStarted = false;
		inRestartWindow = false;
	}

	public void RespawnBall()
	{
		if (!(rocketLeagueBall == null))
		{
			if (rocketLeagueBall.ballPhotonView.isMine)
			{
				rocketLeagueBall.transform.position = rocketLeagueBallSpawnPoint.position;
				rocketLeagueBall.transform.rotation = rocketLeagueBallSpawnPoint.rotation;
			}
			rocketLeagueBall.ballRigidbody.velocity = Vector3.zero;
			rocketLeagueBall.ballRigidbody.angularVelocity = Vector3.zero;
			rocketLeagueBall.ballRigidbody.angularDrag = 0f;
			rocketLeagueBall.gameObject.SetActive(true);
			PointerController.instance.AddPointer(rocketLeagueBall.transform, 0f, "ball_icon", false, null, false, true, false);
		}
	}

	public void BallExplosion()
	{
		rocketLeagueBall.gameObject.SetActive(false);
		GameObject explosionGO = UnityEngine.Object.Instantiate(ballExplosionPrefab, rocketLeagueBall.transform.position, Quaternion.identity) as GameObject;
		CameraModeControl.instance.ShakeCameraExplosion(explosionGO.transform, explosionRadius);
		Collider[] array = Physics.OverlapSphere(explosionGO.transform.position, explosionRadius);
		Collider[] array2 = array;
		foreach (Collider collider in array2)
		{
			Rigidbody componentInParent = collider.GetComponentInParent<Rigidbody>();
			if (componentInParent != null)
			{
				float num = 1f - Vector3.Distance(componentInParent.transform.position, explosionGO.transform.position) / explosionRadius;
				Vector3 force = (componentInParent.transform.position - explosionGO.transform.position).normalized * explosionForce * num;
				force.y = 0f;
				componentInParent.AddForceAtPosition(force, componentInParent.transform.position + componentInParent.transform.forward, ForceMode.Impulse);
				componentInParent.AddForceAtPosition(Vector3.up * explosionUp * num, componentInParent.transform.position + componentInParent.transform.forward, ForceMode.Impulse);
			}
		}
		CoroutineRunner.Invoke(3f, delegate
		{
			UnityEngine.Object.Destroy(explosionGO);
		});
	}

	public void Goal(PunTeams.Team scoredBy, int playerID, string playerName)
	{
		if (!inRestartWindow && rocketLeagueStarted && PVPManager.instance.gameIsStarted)
		{
			TDMController.instance.AddTeamPoints(scoredBy, 1);
			PlayerBehavior.MyPlayer.RPC("RocketLeagueGoalRPC", PhotonTargets.All, (int)scoredBy, playerID, playerName);
		}
	}

	public void OnGoal(PunTeams.Team scoredBy, int playerID, string playerName)
	{
		if (inRestartWindow || !rocketLeagueStarted)
		{
			return;
		}
		if (TDMController.myTeam == scoredBy)
		{
			goalObjectRed.SetActive(true);
			CoroutineRunner.Invoke(3f, delegate
			{
				goalObjectRed.SetActive(false);
			});
		}
		else
		{
			goalObjectBlue.SetActive(true);
			CoroutineRunner.Invoke(3f, delegate
			{
				goalObjectBlue.SetActive(false);
			});
		}
		if (PlayerBehavior.MyPlayer.ID == playerID)
		{
			if (TDMController.myTeam == scoredBy)
			{
				PlayerBehavior.MyPlayer.Points++;
				CupsManager.Cups += 5;
				if ((bool)myCar)
				{
					myCar.ApplyCrown();
				}
				goalSeries++;
				switch (goalSeries)
				{
				case 1:
					GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_1, true);
					break;
				case 2:
					GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_2, true);
					break;
				case 3:
					GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_3, true);
					break;
				case 4:
					GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_4, true);
					break;
				case 5:
					GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_5, true);
					break;
				case 6:
					GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_6, true);
					goalSeries = 0;
					break;
				}
				GameInfoWindow.instance.PaintGoalLabel(goldColor, ScriptLocalization.Get("Game_Common/sm_youScoreEnemyGates"));
			}
			else
			{
				CupsManager.Cups -= 5;
				goalSeries = 0;
				GameInfoWindow.instance.PaintGoalLabel(enemyTeamColor, ScriptLocalization.Get("Game_Common/sm_youScoreYourGates"));
			}
		}
		else
		{
			goalSeries = 0;
			if (TDMController.myTeam == scoredBy)
			{
				GameInfoWindow.instance.PaintGoalLabel(myTeamColor, playerName + " " + ScriptLocalization.Get("Game_Common/sm_RocketLeague_goal"));
			}
			else
			{
				GameInfoWindow.instance.PaintGoalLabel(enemyTeamColor, playerName + " " + ScriptLocalization.Get("Game_Common/sm_RocketLeague_goal"));
			}
		}
		lastGoalTime = Time.unscaledTime;
		BallExplosion();
		PlayerBehavior.MyPlayer.FirstSpawn(3f);
		CoroutineRunner.Invoke(3f, delegate
		{
			StartNewRound(3f);
		});
	}

	public void GoalSave(PunTeams.Team scoredBy)
	{
		if (!inRestartWindow && rocketLeagueStarted && PVPManager.instance.gameIsStarted && !(Time.unscaledTime - lastGoalTime < 1f) && TDMController.myTeam == scoredBy)
		{
			GameplayRewardManager.instance.AddToRewardQueue(rocketLeagueGoal_Save, true);
		}
	}

	public void StartNewRound(float timer = 1.1f)
	{
		if (rocketLeagueBall == null)
		{
			return;
		}
		rocketLeagueBall.transform.position = rocketLeagueBallSpawnPoint.position;
		rocketLeagueBall.transform.rotation = rocketLeagueBallSpawnPoint.rotation;
		if (timer > 1f)
		{
			if ((bool)myCar)
			{
				myCar.RCCcar.EngineToggle(false);
			}
			StartGameWindow.instance.StartTimer(timer, false);
			CoroutineRunner.Invoke(timer, delegate
			{
				RespawnBall();
			});
			CoroutineRunner.Invoke(timer - 1f, delegate
			{
				if ((bool)myCar)
				{
					myCar.RCCcar.EngineToggle(true);
				}
			});
		}
		else
		{
			RespawnBall();
			if ((bool)myCar)
			{
				myCar.RCCcar.EngineToggle(true);
			}
		}
	}

	public void RepositionCar(SpawnPointChief.SpawnPoint point)
	{
		if (!PlayerBehavior.MyPlayer.inCar)
		{
			SpawnCar(point);
		}
		else
		{
			PrivateCarManager.instance.currentCar.rigidCar.isKinematic = true;
			PrivateCarManager.instance.currentCar.rigidCar.velocity = Vector3.zero;
			PrivateCarManager.instance.currentCar.transform.position = point.position;
			PrivateCarManager.instance.currentCar.transform.rotation = point.rotation;
			PrivateCarManager.instance.currentCar.rigidCar.isKinematic = false;
		}
		StartCoroutine(RotateStadium());
		CoroutineRunner.Invoke(4f, delegate
		{
			if ((bool)myCar)
			{
				myCar.RCCcar.EngineToggle(true);
			}
		});
	}

	private IEnumerator RotateStadium()
	{
		while (TDMController.myTeam == PunTeams.Team.none)
		{
			yield return null;
		}
		if (TDMController.myTeam == PunTeams.Team.red)
		{
			instance.rocketLeagueStadium.transform.eulerAngles = new Vector3(0f, 180f, 0f);
		}
		else
		{
			instance.rocketLeagueStadium.transform.eulerAngles = new Vector3(0f, 0f, 0f);
		}
	}

	public void SpawnCar(SpawnPointChief.SpawnPoint point)
	{
		if (myCar == null)
		{
			PrivateCarManager.instance.CarInstantiate(settings.RaceCarName);
			myCar = (CarBehavior)PrivateCarManager.instance.currentCar;
		}
		PrivateCarManager.instance.currentCar = myCar;
		PrivateCarManager.instance.CallCar(point, false);
		Invoke("GetIn", 0.1f);
	}

	private void GetIn()
	{
		PlayerBehavior.MyPlayer.GetInCarWithConfirmation(PlayerBehavior.MyPlayer.viewId, PrivateCarManager.instance.currentCar.viewId);
	}

	private void OnEnable()
	{
		PVPManager.onPVPStarted += StartGame;
	}

	private void OnDisable()
	{
		PVPManager.onPVPStarted -= StartGame;
	}

	private void Update()
	{
		if (GameController.instance.curTypeGame != TypeGame.RocketLeague)
		{
			base.enabled = false;
		}
		else if (Mathf.Abs(rocketLeagueBall.transform.localPosition.x) > ballSpace.x / 2f)
		{
			RespawnBall();
		}
		else if (Mathf.Abs(rocketLeagueBall.transform.localPosition.y) > ballSpace.y / 2f)
		{
			RespawnBall();
		}
		else if (Mathf.Abs(rocketLeagueBall.transform.localPosition.z) > ballSpace.z / 2f)
		{
			RespawnBall();
		}
	}
}
